package com.keyancompany.zhongbaopingtai.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.CircleThemeAdapter;
import com.keyancompany.zhongbaopingtai.api.CircleAPI;
import com.keyancompany.zhongbaopingtai.api.CollectionAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.SearchRecordModel;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;
import com.keyancompany.zhongbaopingtai.weight.IListView;
import com.keyancompany.zhongbaopingtai.weight.ISwipeRefreshLayout;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * 圈子话题
 */
public class CircleThemeActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.filter_edit)
    CustomEditText filterEdit;
    @Bind(R.id.listView)
    IListView listView;
    @Bind(R.id.swipe_layout)
    ISwipeRefreshLayout swipeLayout;
    @Bind(R.id.empty_view)
    LinearLayout emptyView;
    @Bind(R.id.filter_edit_parent)
    LinearLayout filterEditParent;

    CircleThemeAdapter themeAdapter;
    private int total, currentPage;
    private String pid;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_search_circle_theme;
    }

    @Override
    protected int getMenuId() {
        return R.menu.publish_package_menu;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, "圈子话题");
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_publish_package:
//                        T.showShort(mContext, "发布");
                        Intent intent = new Intent(CircleThemeActivity.this, CreateCircleThemeActivity.class);
                        intent.putExtra(Constants.TEAM_ID, pid);
                        startActivityForResult(intent, 200);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    protected void setUpViews() {
        initSwipeLayout(swipeLayout);
        pid = getIntent().getStringExtra(Constants.TEAM_ID);
        filterEditParent.setVisibility(View.GONE);
        themeAdapter = new CircleThemeAdapter(this);
        themeAdapter.setType("circle", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchRecordModel model = (SearchRecordModel) view.getTag();
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.ITEM, model);
                bundle.putString(Constants.TEAM_ID, model.getPid());
                IntentUtils.startActivity(mContext, CircleThemeDetailActivity.class, bundle);
            }
        });
        listView.setAdapter(themeAdapter);
        listView.setStateListener(new IListView.SwipeLayoutState() {
            @Override
            public boolean isRefresh() {
                return swipeLayout.isRefreshing();
            }

            @Override
            public boolean isLastPage() {
                return total == currentPage;
            }

            @Override
            public void requestNextPage() {
                requestTask(++currentPage);
            }
        });
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));
        swipeLayout.setOnRefreshListener(this);


    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        requestTask(currentPage);
    }

    public void requestTask(int currentPage) {

        CircleAPI.getAllTopic(pid, currentPage + "", this, getLoading(-1));
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        swipeLayout.setRefreshing(false);
        if (currentPage == 1) {
            themeAdapter.clear();
//            mAdapter.addAllAfter();
        }
        listView.loadMoreFinish();
        BaseModel base = validateJson(json);
        if (base != null) {
            JSONObject result = JSON.parseObject(base.data);
            total = result.getInteger("pageCount");
            if (total == currentPage)
                listView.removeFooterView();
            List<SearchRecordModel> items = JSONArray.parseArray(result.getString(Constants.LIST), SearchRecordModel.class);
            themeAdapter.addAllAfter(items);
        }

        isEmpty();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            swipeLayout.post(new Thread(new Runnable() {

                @Override
                public void run() {
                    swipeLayout.setRefreshing(true);
                    onRefresh();
                }
            }));
        }
    }

    public void isEmpty() {
        if (themeAdapter.getCount() < 1) {
            emptyView.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        swipeLayout.setRefreshing(false);
    }
}
